#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <conio.h>
using namespace std;
short status;
typedef struct Project
{
    long projectId;
    char projectName[999];
    char projectUserName[432];
    short projectStatus; //0为未开始，1为进行中，2为已完成
    short projectAwards; //0为未获奖，1为1等奖，2为2等奖，3为三等奖
    struct Project *nextProject;
} Project, *ProjectList;

typedef struct Thesis
{
    long thesisId;
    char thesisName[999];
    char thesisUserName[432];
    short thesisStatus; //0为未开始，1为进行中，2为已完成
    short thesisAwards; //0为未获奖，1国家级，2为省部级，3为市厅级,4为校级
    short thesisType;   //0为SCI，1为EI，2为核心，3为一般
    struct Thesis *nextThesis;
} Thesis, *ThesisList;
typedef struct Essay
{
    long essayId;
    char essayName[999];
    struct Essay *nextEssay;
    // Essay(char x[999]) : essayName(x[999]), nextEssay(NULL) {}
} Essay, *EssayList;

typedef struct User
{
    long id;
    char name[10]; //姓名
    User *nextUser;
    Project *nextProject;
    Thesis *nextThesis;
    Essay *nextEssay;
    // User(char name[10])
    // { //显式构造函数,每次对新声明的节点类的对象进行初始化
    //     name = name;
    //     nextUser = NULL;
    //     nextProject = NULL;
    //     nextThesis = NULL;
    //     nextEssay = NULL;
    // }

} User, *UserList;

UserList InitUser()
{
    UserList head = (UserList)malloc(sizeof(User));
    head->nextUser = NULL;
    head->nextProject = NULL;
    head->nextThesis = NULL;
    head->nextEssay = NULL;
    head->id = 0;
    if (NULL == head)
    {
        printf("链表动态分配失败！程序终止！\n");
        exit(-1);
    }
    return head;
}
bool AddUser(UserList head, long id, char name[10])
{
    if (head == NULL)
    {
        return false;
    }
    UserList user = (UserList)malloc(sizeof(User));
    UserList temp = (UserList)malloc(sizeof(User));
    user->id = id;
    strcpy(user->name,name);
    // essay->essayName[999] = essayName;
    if (head->nextUser == NULL)
    {
        head->nextUser = user;
        user->nextUser = NULL;
    }
    else
    {
        temp = head->nextUser;
        while (temp->nextUser != NULL)
        {
            temp = temp->nextUser;
        }
        temp->nextUser = user;
        user->nextUser = NULL;
    }
    return true;
}
// {
//     UserList user = (UserList)malloc(sizeof(User));
//     UserList temp = (UserList)malloc(sizeof(User));
//     user->id = id;
//     strcpy(user->name,name);
//     if (head->nextUser == NULL)
//     {
//         head->nextUser = user;
//     }
//     else
//     {
//         temp = head->nextUser;
//         if (temp->nextUser == NULL)
//         {
//             temp->nextUser = user;
//         }else
//         {
//         while (head->nextUser != NULL)
//         {
//             temp = temp->nextUser;
//         }
//         temp->nextUser = user;
//         }
//     }
//     user->nextUser = NULL;
//     user->nextProject = NULL;
//     user->nextThesis = NULL;
//     user->nextEssay = NULL;
//     return true;
// }
bool AddProject(UserList user,long projectId, char projectName[999],
                char projectUserName[432],
                short projectStatus,
                short projectAwards)
{
    if (user == NULL)
    {
        return false;
    }
    ProjectList project = (ProjectList)malloc(sizeof(Project));
    ProjectList temp = (ProjectList)malloc(sizeof(Project));
    project->projectId = projectId;
    strcpy( project->projectName,projectName);
    strcpy(project->projectUserName,projectName);
    // project->projectName[999] = projectName[999];
    // project->projectUserName[432] = projectUserName[432];
    project->projectStatus = projectStatus;
    project->projectAwards = projectAwards;
    if (user->nextProject == NULL)
    {
        user->nextProject = project;
        project->nextProject = NULL;
    }
    else
    {
        temp = user->nextProject;
        while (temp->nextProject != NULL)
        {
            temp = temp->nextProject;
        }
        temp->nextProject = project;
        project->nextProject = NULL;
    }
    return true;
}
bool AddThesis(UserList user,long thesisId, char thesisName[999],
               char thesisUserName[432],
               short thesisStatus,
               short thesisAwards,
               short thesisType)
{
    if (user == NULL)
    {
        return false;
    }
    ThesisList thesis = (ThesisList)malloc(sizeof(Thesis));
    ThesisList temp = (ThesisList)malloc(sizeof(Thesis));
    thesis->thesisId = thesisId;
    // thesis->thesisName[999] = thesisName[999];
    // thesis->thesisUserName[432] = thesisUserName[432];
    strcpy( thesis->thesisName,thesisName);
    strcpy(thesis->thesisUserName,thesisUserName);
    thesis->thesisStatus = thesisStatus;
    thesis->thesisAwards = thesisAwards;
    thesis->thesisType = thesisType;
    if (user->nextThesis == NULL)
    {
        user->nextThesis = thesis;
        thesis->nextThesis = NULL;
    }
    else
    {
        temp = user->nextThesis;
        while (temp->nextThesis != NULL)
        {
            temp = temp->nextThesis;
        }
        temp->nextThesis = thesis;
        thesis->nextThesis = NULL;
    }
    return true;
}
bool AddEssay(UserList user, long essayId, char essayName[999])
{
    if (user == NULL)
    {
        return false;
    }
    EssayList essay = (EssayList)malloc(sizeof(Essay));
    EssayList temp = (EssayList)malloc(sizeof(Essay));
    essay->essayId = essayId;
    strcpy(essay->essayName,essayName);
    // essay->essayName[999] = essayName;
    if (user->nextEssay == NULL)
    {
        user->nextEssay = essay;
        essay->nextEssay = NULL;
    }
    else
    {
        temp = user->nextEssay;
        while (temp->nextEssay != NULL)
        {
            temp = temp->nextEssay;
        }
        temp->nextEssay = essay;
        essay->nextEssay = NULL;
    }
    return true;
}

User SearchUser(UserList head,long id){
    UserList temp = (UserList)malloc(sizeof(User));
     if (head->nextUser == NULL || head == NULL)
    {
        status = 0;
        exit(-1);
    }
    else
    {
        
        temp = head->nextUser;
        if (temp->id == id)
        {
            status = 1;
            return *temp;
        }
        while (temp->nextUser != NULL)
        {
            temp = temp->nextUser;
            if (temp->id == id)
            {
                status = 1;
                return *temp;
            }
        }
    }
    status = 0;
}

bool DeleteUser(UserList head, long id){
    UserList lastTemp = (UserList)malloc(sizeof(User));
    UserList temp = (UserList)malloc(sizeof(User));
     if (head->nextUser == NULL || head == NULL)
    {
        status = 0;
        exit(-1);
    }
    else
    {
        temp = head->nextUser;
        if (temp->id == id)
        {
            head->nextUser = temp->nextUser;
            free(temp);
            return true;
        }
        lastTemp = temp;
        while (temp->nextUser != NULL)
        {
            temp = temp->nextUser;
            if (temp->id == id)
            {
                lastTemp->nextUser = temp->nextUser;
                free(temp);
                return true;
            }
            lastTemp = temp;
        }
    }
    return false;
}

int main(int argc, char const *argv[])
{
    string strr;
    UserList head;
    string str = "sdad";
    User user;
    strr = str;
    cout<<strr;
    char name[10] = "fadsf";
    // char essayName[999] = "trweyusfdghfhjdfghj";
    head = InitUser();
    AddUser(head,230201, name);
    AddUser(head,33020, name);
    AddUser(head,330201, name);
    AddUser(head,430201, name);
    AddUser(head,530201, name);
    // AddUser(head,430201, name);
    // AddUser(head,530201, name);
    // AddUser(head,630201, name);
    // AddUser(head,230201, name);
    // AddUser(head,230201, name);
    // AddEssay(temp,54, essayName);
    // AddEssay(temp,4354, essayName);
    // AddEssay(temp,543, essayName);
    // AddProject(temp, 453 ,essayName, essayName, 3, 2);
    // AddProject(temp, 432,essayName, essayName, 3, 2);
    // AddProject(temp, 423,essayName, essayName, 3, 2);
    // AddThesis(temp, 423,essayName, essayName, 3, 2, 4);
    // AddThesis(temp, 423,essayName, essayName, 3, 2, 2);
    // AddThesis(temp, 645,essayName, essayName, 3, 2, 4);
    user = SearchUser(head,33020);
    if ( status ==1 )
    {
        printf("%d",user.id);
        printf("成功");
    }else
    {
        printf("失败");
    }


    if ( DeleteUser(head,33020) )
    {
        printf("成功");
    }else
    {
        printf("失败");
    }


    user = SearchUser(head,33020);
    if ( status ==1 )
    {
        printf("%d",user.id);
        printf("成功");
    }else
    {
        printf("失败");
    }
    free(head);
    system("pause");
    return 0;
}
